package com.eascs.paymentdetail.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.Format;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.DefaultRowWriteHandler;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.IoUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.paymentdetail.service.O2oTransferAccountService;
import com.eascs.paymentdetail.vo.O2oTransferAccountVo;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.tag.MenuTag;


/**
 * @ClassName: O2oTransferAccountsController
 * @Description: 付款转账管理 
 * @author 张庆奇
 * @date 2017年5月16日 上午10:08:32
 * @version V1.0
 */
@Controller
@RequestMapping("/transferAccounts")
public class O2oTransferAccountsController extends BaseMasterViewController {
	
	@Autowired
	O2oTransferAccountService o2oTransferAccountService;

	/**
	 * @Title: index
	 * @Description: 付款转账列表信息
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws
	 * @date 2017年5月18日 下午3:41:01
	 */
	@RequestMapping("/index")
	public String index(O2oTransferAccountVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "TransferAccount");
		vo.setPageSize(10);
		String pageUrl = "/transferAccounts/index";
		String reporto2ourl = "/transferAccounts/download";
		pageUrl = UrlUtil.appendUrl(pageUrl, "billCode", vo.getBillCode() == null ? "" : vo.getBillCode());
		pageUrl = UrlUtil.appendUrl(pageUrl, "collectionUserName", vo.getCollectionUserName() == null ? "" : vo.getCollectionUserName());
		pageUrl = UrlUtil.appendUrl(pageUrl, "billStatus", vo.getBillStatus()== null ? "" : vo.getBillStatus());
		pageUrl = UrlUtil.appendUrl(pageUrl, "startDate", vo.getStartDate() == null ? "" : vo.getStartDate());
		pageUrl = UrlUtil.appendUrl(pageUrl, "endDate", vo.getEndDate() == null ? "" : vo.getEndDate());
		pageUrl = pageUrl + "&pageIndex=";
		
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "billCode", vo.getBillCode() == null ? "" : vo.getBillCode());
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "collectionUserName", vo.getCollectionUserName() == null ? "" : vo.getCollectionUserName());
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "billStatus", vo.getBillStatus()== null ? "" : vo.getBillStatus());
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "startDate", vo.getStartDate() == null ? "" : vo.getStartDate());
		reporto2ourl = UrlUtil.appendUrl(reporto2ourl, "endDate", vo.getEndDate() == null ? "" : vo.getEndDate());
		
		PageResult<Object[]> pageResult = o2oTransferAccountService.findByPage(vo);
		String titles[] = { "单据号", "收款户名", "付款金额", "收款账号","收款开户行", "收款行地址","创建人", "创建时间", "单据状态" };
		AjaxButton button = new AjaxButton(null, "/transferAccountsRest/pay_approval?referId=");
		/*Button start = ButtonHelper.createAjaxButton("发起付款流程", "/transferAccountsRest/startPay?id=", "startPayButton");
		start.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[8].equals("新建")||params[8].equals("驳回")) {
					return true;
				} else
					return false;
			}
		});
		
		
		Button delButton = ButtonHelper.createAjaxButton("删除", "/transferAccountsRest/delete?id=", "startPayButton");
		delButton.setShowButtonFilter(new ShowButtonFilter() {

			@Override
			public boolean filter(Object param) {
				Object[] params = (Object[]) param;
				if (params[8].equals("新建")) {
					return true;
				} else
					return false;
			}
		});*/
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		Button[] titleButtons = new Button[] {button };
		//Button[] moreButtons = new Button[] { delButton,start };
		paginationInfo.setTitleButtons(titleButtons);
		//paginationInfo.setMoreButtons(moreButtons);
		paginationInfo.setFormats(new Format[] { null, null, new DecimalFormat(",##0.00"),null,  null, null, null, null,null });
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		ViewContext.put("reporto2ourl", reporto2ourl);
		return "paymentdetail/transfer_accounts";
	}
	
	/**
	 * @Title: dowanload
	 * @Description: 财务明细导出
	 * @author 张庆奇
	 * @param vo
	 * @param httpServletResponse
	 * @param request
	 * @return void
	 * @throws
	 * @date 2017年3月28日 上午9:34:21
	 */
	@RequestMapping("/download")
	public void dowanload(O2oTransferAccountVo vo, HttpServletResponse httpServletResponse, HttpServletRequest request) {
		vo.setPageSize(80000);
		vo.setFlag("export");
		PageResult<Object[]> pageResult = o2oTransferAccountService.findByPage(vo);
		String path = O2oTransferAccountsController.class.getResource("/excelTemplate").getPath();
		StringBuffer s = new StringBuffer();
		String outPath = s.append(path).append("/transferAccounts_model.xls").toString();
		OutputStream os = null;
		try {
			String outfile = "付款转账数据.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists())
				throw new RuntimeIOException("file not found");
			os = httpServletResponse.getOutputStream();
			httpServletResponse.reset();
			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");
			DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), O2oTransferAccountsController.class.getResourceAsStream("/excelTemplate/transferAccounts_model.xls"), defaultRowWriteHandler);
			ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
			IoUtil.fileToOutputStream(f, byteArrayOutputStream);
			os.write(byteArrayOutputStream.toByteArray());
			os.flush();
			os.close();
		} catch (Exception e) {
			throw new RuntimeIOException(e);
		} finally {
			IoUtil.close(os);
		}
	}

}
